package pm.oo.util;

import com.sun.star.comp.helper.BootstrapException;
import ooo.connector.BootstrapSocketConnector;

/**
 * support ONE singleton uno connection to an running office installation!
 * Can be used to open/use/close connection to uno environment of an office. If
 * necessary a new office instance is started.
 */
public class OfficeConnection {

	private static com.sun.star.uno.XComponentContext officeContext;
	private static BootstrapSocketConnector bootstrapSocketConnector;

	public static synchronized void createConnection(final String oooExecFolder) throws BootstrapException {
		if (officeContext == null) {
			bootstrapSocketConnector = new BootstrapSocketConnector(oooExecFolder);
			officeContext = bootstrapSocketConnector.connect();
//			officeContext = BootstrapSocketConnector.bootstrap(ooExeFolder);
//			officeContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
		}
	}

	public static synchronized void disconnect() {
		if (officeContext != null) {
			officeContext = null;
		}
		if (bootstrapSocketConnector != null) {
			bootstrapSocketConnector.disconnect();
		}
	}

	private OfficeConnection() {
	}

	/**
	 * returns remote component context of the connected office
	 */
	public static synchronized com.sun.star.uno.XComponentContext getOfficeContext() {
		return officeContext;
	}
}

